<template>
	<view>
		<view class="padding-xl">
			<view class="mb-2" v-for="(item,index) in serviceMenu" :key="index">
				{{item | tostring}}
			</view>
			<view class="mb-2" v-for="(item,index) in conditionalmenu" :key="index">
				{{item | tostring}}
				<view class="cu-tag bg-orange round m-1" @click="delpro(item)">删除</view>
			</view>
			<button class="mt-2 block bg-green  lg  cu-btn round" @click="add">创建自定义菜单</button>
			<button class="mt-2 block bg-orange  lg  cu-btn round" @click="del">删除自定义菜单</button>
			<button class="mt-2 block bg-olive  lg  cu-btn round" @click="get">查询自定义菜单</button>
			<button class="mt-2 block bg-green  lg  cu-btn round" @click="addpro">创建个性自定义菜单</button>

			<button class="mt-2 block bg-olive  lg  cu-btn round" @click="testpro">测试个性自定义菜单</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				menu: {
					"button": [{
							"type": "view",
							"name": "测试",
							"url": "http://www.baidu.com/"
						},
						{
							"name": "菜单",
							"sub_button": [{
									"type": "view",
									"name": "搜索",
									"url": "http://www.soso.com/"
								},
								{
									"type": "view",
									"name": "视频",
									"url": "http://v.qq.com/"
								},
								{
									"type": "view",
									"name": "百度",
									"url": "http://www.baidu.com/"
								}
							]
						}
					]
				},
				promenu: {
					"button": [{
							"type": "view",
							"name": "测试",
							"url": "http://www.baidu.com/"
						},
						{
							"name": "菜单",
							"sub_button": [{
									"type": "view",
									"name": "搜索",
									"url": "http://www.soso.com/"
								},
								{
									"type": "view",
									"name": "视频",
									"url": "http://v.qq.com/"
								},
								{
									"type": "view",
									"name": "百度",
									"url": "http://www.baidu.com/"
								}
							]
						},
						{
							"type": "click",
							"name": "赞一下我们",
							"key": "V1001_GOOD"
						}
					],
					"matchrule": {
						"country": "中国",
						"province": "贵州",
						"city": "贵阳",
					}
				},
				serviceMenu: [],
				conditionalmenu: []
			}
		},
		filters: {
			tostring(value) {
				return JSON.stringify(value)
			}
		},
		onLoad() {
			this.get()
		},
		methods: {
			// 添加自定义菜单
			add() {
				uniCloud.callFunction({
					name: 'wxAction',
					data: {
						action: 'menu',
						options: 'add',
						params: this.menu
					}
				}).then(res => {
					if (res.result.status === 200) {
						uni.showToast({
							title: '添加菜单成功',
							icon: 'none'
						});
						this.get()
					}
				})
			},
			// 删除自定义菜单
			del() {

				uni.showModal({
					title: '危险操作',
					content: '即将删除自定义菜单',
					success: result => {
						if (result.confirm) {
							uniCloud.callFunction({
								name: 'wxAction',
								data: {
									action: 'menu',
									options: 'del',
									params: {}
								}
							}).then(res => {
								console.log(res)
								if (res.result.status === 200) {
									uni.showToast({
										title: '删除菜单成功',
										icon: 'none'
									});
									this.get()
								}

							})
						}
					},
					fail: () => {},
					complete: () => {}
				});


			},
			// 获取自定义菜单
			get() {
				uniCloud.callFunction({
					name: 'wxAction',
					data: {
						action: 'menu',
						options: 'get',
						params: {}
					}
				}).then(res => {
					if (res.result.status === 200) {
						uni.showToast({
							title: '获取菜单成功',
							icon: 'none'
						});

						let {
							menu,
							conditionalmenu
						} = res.result.data
						this.serviceMenu = menu ? menu : [],
							this.conditionalmenu = conditionalmenu ? conditionalmenu : []
					}
					console.log(res)
				})
			},
			// 添加自定义菜单
			addpro() {
				uniCloud.callFunction({
					name: 'wxAction',
					data: {
						action: 'promenu',
						options: 'add',
						params: this.promenu
					}
				}).then(res => {
					if (res.result.status === 200) {
						uni.showToast({
							title: '添加个性菜单成功',
							icon: 'none'
						});
						this.get()
					}
				})




			},
			// 删除自定义菜单
			delpro(item) {
				let menuid = item.menuid
				uni.showModal({
					title: '危险操作',
					content: '即将删除自定义菜单',
					success: result => {
						if (result.confirm) {
							uniCloud.callFunction({
								name: 'wxAction',
								data: {
									action: 'promenu',
									options: 'del',
									params: {
										menuid: menuid
									}
								}
							}).then(res => {
								console.log(res)
								if (res.result.status === 200) {
									uni.showToast({
										title: '删除个性菜单成功',
										icon: 'none'
									});
									this.get()
								}

							})
						}
					},
					fail: () => {},
					complete: () => {}
				});

			},
			// 测试自定义个性菜单
			testpro() {
				uniCloud.callFunction({
					name: 'wxAction',
					data: {
						action: 'promenu',
						options: 'test',
						params: {
							user_id: "Hack520951205"
						}
					}
				}).then(res => {
					if (res.result.status === 200) {
						uni.showToast({
							title: '获取菜单成功',
							icon: 'none'
						});
						this.serviceMenu = res.result.data
					}
					console.log(res)
				})
			}
		}
	}
</script>

<style>

</style>
